<?php

namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\Db;
use think\db\Query;

class RoomController extends AdminBaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $school_id = $this->request->param('school_id');
        $name = $this->request ->param('name');
        $room = Db :: name('room')
            ->join('cmf_school','cmf_school.id=cmf_room.sid','LEFT')
            ->join('cmf_student s','s.cid=cmf_room.id','LEFT')
            ->join('cmf_record','s.id=cmf_record.student_id','LEFT')
            ->join('cmf_score sc','cmf_record.score_id=sc.id','LEFT')
            ->where(function(Query $query) use ($school_id,$name){
                if($school_id){
                    $query->where('sid',$school_id);
                }
                if($name){
                    $query -> where('cmf_room.name','like',"%$name%");
                }
            })
            ->field('cmf_room.*,cmf_school.name as school_name')
            ->field('sum(sc.name) as total')
            ->group('cmf_room.id')
            ->order('id desc')
            ->paginate('3',false,['query'=>$this->request->param()]);
        $room->appends(['school_id'=>$school_id,'cmf_room.name'=>$name]);   
        $school = Db::name('school')->select();
        $page=$room->render();
        return $this->fetch('',[
            'room'=>$room,
            'school'=>$school,
            'page'=>$page,
            'school_id'=>$school_id]);
    }

    /**
     * 显示添加资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function add()
    {
        $school = Db::name('school')->select();
        return $this->fetch('',['school'=>$school]);
    }
    public function addPost()
    {
        if(request()->isPost()){
            $date=input();
            // halt($date);
            if(empty($date['name'])){
                $this->error('班级不能为空');
            }
            $hasRoom = Db::name('room')->where(['name'=>$date['name']])->find();
            if($hasRoom){
                $this->error('班级已存在');
            }
            $add = Db::name('room')->insert($date);
            if($add)
                $this->success('添加成功','room/index');
                $this->error('添加失败');
        }
    }


   

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $school = Db ::name('school')->select();
        $room = Db ::name('room')->get($id);
        return $this->fetch('',['school'=>$school,'room'=>$room]);
    }
    public function editPost()
    {
        if(request()->isPost()){
            // halt(input());
            if(empty(input('name'))){
                $this->error('班级不能为空');
            }
            $hasRoom = Db::name('room')->where(['name'=>input('name')])->find();
            if($hasRoom){
                $this->error('班级已存在');
            }
            $edit = Db :: name('room')->update(input());
            if($edit){
                $this->success('修改成功','room/index');
            }$this->error('修改失败');
        }
    }

    

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $del = Db::name('room')->where('id',$id)->delete();
        if($del)
            $this->success('删除成功','room/index');
            $this->error('删除失败');
    }

    public function delAll()
    {
        $ids = input('ids/a');
        $del = Db::name('room')->delete($ids);
        if($del)
            $this->success('删除成功','room/index');
            $this->error('删除失败');
    }

    public function mingxi($id)
    {
        $mingxi = Db::name('record r')
            ->join('cmf_student s','s.id=r.student_id')
            ->join('cmf_room','s.cid=cmf_room.id')
            ->join('cmf_score sc','sc.id=r.score_id')
            ->join('cmf_reason re','re.id=r.reason_id')
            ->where('cmf_room.id',$id)
            ->field('r.*,s.name as student_name,sc.name as score_name,re.name as reason_name')
            ->field('cmf_room.name as room_name')
            ->select();
            //halt($mingxi);
        return $this->fetch('',['mingxi'=>$mingxi]);
    }

}
